﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    ItemBaseEditor(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    table = TD_PRODUCT;
    itemModel=new QStandardItemModel(this);

    ui->treeView->setModel(itemModel);
    ui->treeView->setEditTriggers(QTreeView::NoEditTriggers);

    loadLocation();

    updateView();
    createMenu();
}

Widget::~Widget()
{
    delete ui;

    saveLocation();
}

QList<QStandardItem *> Widget::getLabelItems(const QString &category) const
{
    QList<QStandardItem *> items;
    QSqlQuery query;
    QString sql=QString("select label from %1 where category = '%2'").arg(table).arg(category);
    query.exec(sql);
    while(query.next())
    {
        QString label=query.value(0).toString();
        items.append(new QStandardItem(label));
    }

    return items;
}

void Widget::updateView()
{
    updateView_One(QStringLiteral("类目"));
    ui->treeView->resizeColumnToContents(0);
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("规格查询..."),this,&Widget::searchSpec);
    menu->addAction(QStringLiteral("添加..."),this,&Widget::newItem);
    menu->addAction(QStringLiteral("编辑..."),this,&Widget::editItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("【删除】"),this,&Widget::removeItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新"),this,&Widget::updateView);
    menu->addAction(QStringLiteral("全部折叠"),this,&Widget::collapseAll);
}

void Widget::searchSpec()
{
    showSearchBox("addition");
}

void Widget::newItem()
{
    ItemAdd itemAdd;
    if(itemAdd.exec() == QDialog::Accepted)
        updateView();
}

void Widget::removeItem()
{
    QModelIndex index=ui->treeView->currentIndex();
    if(!index.isValid())
        return;

    QModelIndex p_index=index.parent();
    if(p_index == QModelIndex())
        return;

    QMessageBox::StandardButton btn=QMessageBox::warning(this,QStringLiteral("操作确认"),QStringLiteral("确定删除？无法撤销！"),
                                                         QMessageBox::Cancel | QMessageBox::Ok,
                                                         QMessageBox::Cancel );
    if(btn == QMessageBox::Ok)
    {
        QString category=model->data(p_index).toString();
        QString label=model->data(index).toString();
        QSqlQuery query;
        QString sql=QString("delete from %1 where category='%2' and label='%3'")
                .arg(table)
                .arg(category)
                .arg(label);
        if(query.exec(sql))
            updateView();
        else
            QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，删除失败！"));
    }
}

void Widget::editItem()
{
    QModelIndex index=ui->treeView->currentIndex();
    if(!getItemByIndex(index))
        return;

    QSqlQuery query;
    QString sql=QString("select id from %1 where category='%2' and label='%3'")
            .arg(table)
            .arg(category)
            .arg(label);

    query.exec(sql);
    if(query.next())
    {
        int id=query.value("id").toInt();
        ItemEdit itemEdit(id);
        if(itemEdit.exec() == QDialog::Accepted)
            updateView();
    }
}

void Widget::collapseAll()
{
    ui->treeView->collapseAll();
}

void Widget::on_treeView_doubleClicked(const QModelIndex &index)
{
    if(!getItemByIndex(index))
        return;

    ImageViewer viewer(0,label);
    if(!viewer.isEmpty())
    {
        viewer.viewFirst();
        viewer.exec();
    }
}

void Widget::on_treeView_clicked(const QModelIndex &index)
{
    if(!getItemByIndex(index))
        return;

    if(!getDetail())
        return;

    ui->intro->setPlainText(detail);
    ui->spec->setPlainText(addition);
}

void Widget::on_input_returnPressed()
{
    QString keyword=ui->input->text().trimmed();
    if(keyword.isEmpty())
        return;

    showSearchView(keyword);
    ui->input->clear();
}
